package com.fjnu;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.sql.Connection;
import java.sql.DriverManager;

/**
 * @desc: Spring配置类
 * @author:
 * @date:
 */
@Configuration
@ComponentScan(basePackages = "com.fjnu")
@PropertySource("db.properties")
public class SpringConfig {
    @Value("${sqlserver.driver}")
    private String driver;
    @Value("${sqlserver.url}")
    private String url;
    @Value("${sqlserver.username}")
    private String username;
    @Value("${sqlserver.password}")
    private String password;

    @Bean
    public Connection connection() {
        try {
            Class.forName(driver);
            System.out.println("数据库连接成功！");
            return DriverManager.getConnection(url, username, password);
        } catch (Exception e) {
            System.err.println("数据库连接失败: " + e.getMessage());
            e.printStackTrace();
            return null;
        }
    }
}